package com.armani.controller;

import com.alibaba.fastjson.JSON;
import com.armani.entity.Great;
import com.armani.entity.Shopcar;
import com.armani.model.Result;
import com.armani.model.ShopCarModel;
import com.armani.service.GreatService;
import com.armani.service.ShopcarService;
import com.armani.util.JsonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ProjectName: armani
 * @Package: com.armani.controller
 * @ClassName: ShopcarController
 * @Description: TODO
 * @Author: Xinyibuzai
 * @Date: 2021/11/29 14:27
 * @Version: 1.0
 **/
@Controller
@RequestMapping(value = "/shopcar/", produces = "application/json; charset=utf-8")
public class ShopcarController {
    @Autowired
    private ShopcarService shopcarService;

    @GetMapping(value = "search.do")
    @ResponseBody
    private String listSearch(@RequestParam Integer page,
                              @RequestParam Integer limit,
                              @RequestParam(required = false) Integer num) {
        Integer startRow = (page - 1) * limit;

        List<Shopcar> shopcars = shopcarService.listSearch(startRow, limit, num);

        Integer count = shopcarService.getCount(num);

        Result result = new Result();
        result.setData(shopcars);
        result.setCount(count);
        result.setCode(0);

        String s = JSON.toJSONString(result);
        return s;
    }

    @PostMapping(value = "update.do")
    @ResponseBody
    public String update(@RequestParam Integer num,
                         @RequestParam Integer userId,
                         @RequestParam Integer skuId,
                         @RequestParam String insertTime,
                         @RequestParam(required = false) Integer id) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date insertTimes = sdf.parse(insertTime);

        if (id == null) {
            shopcarService.insert(num, userId, skuId, insertTimes);
        } else {
            shopcarService.update(num, userId, skuId, insertTimes, id);
        }
        Result result = new Result();
        result.setCode(0);
        String st = JSON.toJSONString(result);
        return st;
    }

    @PostMapping(value = "delete.do")
    @ResponseBody
    public String delete(@RequestParam Integer id) {
        shopcarService.delete(id);
        Result result = new Result();
        result.setCode(0);
        String str = JSON.toJSONString(result);
        return str;
    }

    /**
     * 查询用户购物车
     * @param userId 用户id
     * @return 购物车列表JSON
     */
    @GetMapping(value = "search/user.do")
    @ResponseBody
    public String searchByUserId(@RequestParam Integer userId) {
        List<ShopCarModel> shopcars = shopcarService.selectByUserId(userId);
        Result result = new Result();
        result.setCode(0);
        result.setMsg("成功");
        result.setData(shopcars);
        String json = JSON.toJSONString(result);
        return json;
    }

    @RequestMapping(value = "edit/num.do")
    @ResponseBody
    public String editNumBySkuId(@RequestParam(required = false) Integer userId,
                                 @RequestParam(required = false) Integer skuId,
                                 @RequestParam(required = false) Integer num){
        Map<String ,Object> params=new HashMap<>();
        params.put("userId",userId);
        params.put("skuId",skuId);
        params.put("num",num);
        Integer row = shopcarService.editNum(params);
        Result result = new Result();

        if (row == 0) {
            result.setCode(1000);
            result.setMsg("失败");
        } else {
            result.setCode(0);
            result.setMsg("成功");
        }
        return JsonUtil.stringify(result);
    }
}
